package titan.wizards;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.wizard.IWizardContainer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.widgets.Composite;

/**
 * All pages of the plugin inherit from this page 
 * The class simplifies the application of a page status
 */
public abstract class BaseTitanPage extends WizardPage {
	protected IStatus pageStatus;

	protected BaseTitanPage(String pageName) {
		super(pageName);
		pageStatus = new Status(IStatus.OK, "default", ""); //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * This method has to be called when the status of the wizard has changed. It applies
	 * the status to the wizard and, if it's not an {@link IStatus#INFO INFO} status, to the pageStatus too.
	 * @param new_status - the new status to apply
	 */
	protected final void applyPageStatus(IStatus new_status) {
		if(new_status.getSeverity() != IStatus.INFO)
			new_status = greaterStatus();

		String message = new_status.getMessage();
			switch (new_status.getSeverity()) {
				case IStatus.OK:
					pageStatus = new_status;
					setMessage(null);
					break;
				case IStatus.INFO:
					setMessage(message, WizardPage.INFORMATION);
					break;
				case IStatus.WARNING:
					pageStatus = new_status;
					setMessage(message, WizardPage.WARNING);
					break;
				default:
					pageStatus = new_status;
					setMessage(message, WizardPage.ERROR);
					break;
			}
	}

	/**
	 * This method has to return the greater status among all the page fields' status
	 * @return the greater status
	 */
	protected abstract IStatus greaterStatus();

	/**
	 * This method has to apply the given status to the owner field of it and call
	 * {@link BaseTitanPage#applyPageStatus(IStatus) applyPageStatus}
	 * @param new_status the status to apply
	 */
	protected abstract void applyStatus(IStatus new_status);

	/**
	 * @see IWizardContainer#updateButtons()
	 */
	public final void updateButtons() {
		getWizard().getContainer().updateButtons();
	}

	/**
	 * {@inheritDoc}
	 */
	public abstract void createControl(Composite parent);

	/**
	 * {@inheritDoc}
	 */
	public abstract boolean canFlipToNextPage();
}